﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.Forum;
using Jalal.Library.Web;
using BCGC.BLL.BusinessObject.UserAccount;
using System.Text;


public partial class start_topic : PublicBasePage
{
    public int PostCategoryID
    {
        get
        {
            if (Request.QueryString["cat"] != null)
                return Convert.ToInt32(Request.QueryString["cat"].ToString());
            else
                return -1;
        }
        
    }

    public bool IsReplyPost
    {
        get
        {
            if (Request.QueryString["replyid"] != null)
                return true;
            else
                return false;
        }
        
    }
    public bool IsQuote
    {
        get
        {
            if (Request.QueryString["quoteid"] != null)
                return true;
            else
                return false;
        }

    }

    public int QuoteID
    {
        get
        {
            if (Request.QueryString["quoteid"] != null)
                return Convert.ToInt32(Request.QueryString["quoteid"].ToString());
            else
                return -1;
        }

    }

    public int ReplyToPost
    {
        get
        {
            if (Request.QueryString["replyid"] != null)
                return Convert.ToInt32(Request.QueryString["replyid"].ToString());
            else
                return 0;
        }
        
    }

    public string UserName
    {
        get
        {
            if (Request.QueryString["userName"] != null)
                return Request.QueryString["userName"].ToString();
            else
                return string.Empty;
        }
        
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!SessionManager.IsUserLoggedIn)
            {
                SessionManager.ReturnUrl = CustomSiteMap.StartTopic;
                Response.Redirect(CustomSiteMap.Login);
            }
            else
            {
                if (IsQuote)
                    PreFillData();
            }

            divPreview.Visible = false;
            
        }
    }

    private void PreFillData()
    {
        if (QuoteID != -1)
        {
            Post post = ForumManager.GetPostByID(QuoteID);
            if (post != null)
            {
                wucPostWriter1.Text = "[quote]" + Server.HtmlDecode(post.PostContent) + "[/quote]";
                txtSubject.Text = Server.HtmlDecode(post.PostTitle);
            }
            
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        wucPostWriter1.Text = "";
        txtSubject.Text = "";
        txtTag.Text = "";
    }

    
    protected void btnPreview_Click(object sender, EventArgs e)
    {
        divPreview.Visible = true;
        if (SessionManager.IsUserLoggedIn)
        {
            litPostDate.Text = string.Format("{0:ddd, dd MMM yyyy hh:mm tt}", DateTime.Now);
            litPostContent.Text = ForumManager.GetFormatedPost(Server.HtmlDecode(wucPostWriter1.Text));
            wucPostUser1.UserID = SessionManager.LoggedUser.Id.ToString();
            wucPostUser1.LoadData();
        }
        else
        {
            Response.Redirect(CustomSiteMap.Login);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string tag =Server.HtmlEncode(txtTag.Text.Trim().ToLower());
            string subject = Server.HtmlEncode(txtSubject.Text.Trim());
            string content = Server.HtmlEncode(wucPostWriter1.Text);
            if (SessionManager.IsUserLoggedIn)
            {
                if (PostCategoryID != -1)
                {
                    if (ForumManager.SavePost(tag, PostCategoryID, SessionManager.LoggedUser.Id, subject, content, DateTime.Now, IsReplyPost, 0, ReplyToPost, true))
                    {
                        rmResult.SetSuccessMessage("Your Post is Saved and Activated");
                    }
                    else
                    {
                        rmResult.SetFailMessage("Failed to Save Post");
                    }
                }
                else
                {
                    rmResult.SetFailMessage("Select Post Category First");
                }
            }
            else
            {
                Response.Redirect(CustomSiteMap.Login);
            }
            
        }
    }
   
}
